import { Component, OnInit } from '@angular/core';
import { PageEntity } from '../../../core/domain/page.entity';
import { LogsService } from './logs.service';
import { FormBuilder, FormGroup } from '@angular/forms';
import { HTTP_BASE_URL, SERVERS_NAME } from '../../../config/config';
import { DatePipe } from '@angular/common';
import { StatusEnum } from '../../../core/enum/status.enum';
import { FilterEnum, FilterTypeEnum } from '../../../core/enum/filter.enum';
import { NzMessageService, UploadFile } from 'ng-zorro-antd';
import { OtherUtil } from '../../../utils/other.util';
import { SourceServer } from '../../../common/source.server';
import { PostDown } from '../../../utils/postdown';

@Component({
  selector: 'app-logs',
  templateUrl: './logs.component.html',
  styleUrls: ['./logs.component.scss'],
  providers: [LogsService, SourceServer, PostDown]
})
export class LogsComponent implements OnInit {
  operator = '';
  moduleName = '';
  operationType = '';
  gmtCreate = null;
  date = null;
  operateList = [
    {
      text: 'create',
      value: '新增'
    }, {
      text: 'update',
      value: '编辑'
    }, {
      text: 'delete',
      value: '删除'
    }, {
      text: 'query',
      value: '查询'
    }, {
      text: 'check',
      value: '审批'
    }, {
      text: 'upload',
      value: '导入'
    }, {
      text: 'download',
      value: '导出'
    },
  ];
  pageInfo: PageEntity = new PageEntity();
  allList = [];
  displayList = [];
  mapOfCheckedId: { [key: string]: boolean } = {};
  isAllCheck = false;
  isIndeterminate = false;
  canDelete = true;

  constructor(private service: LogsService,
    private fb: FormBuilder,
    private datePipe: DatePipe,
    private source: SourceServer,
    private postDown: PostDown,
    private message: NzMessageService) {
  }

  ngOnInit() {
    this.date = this.datePipe.transform(new Date(), 'yyyy-MM-dd');
    this.getPage();
  }

  checkDelete(): void {
    this.canDelete = OtherUtil.deleteIds(this.mapOfCheckedId).length <= 0;
  }
  onChange(result: Date): void {
    this.gmtCreate = result;
  }

  // 点击查询
  clickSearch() {
    this.pageInfo.first = 1;
    const data = this.gmtCreate ? this.gmtCreate : new Date();
    const temp = this.datePipe.transform(data, 'yyyy-MM-dd');
    const result = temp + ' 00:00:00,' + temp + ' 23:59:59';
    this.dealPageFilterFn(this.pageInfo, this.operator, 'operator', FilterEnum.CONTAINS, FilterTypeEnum.AND);
    this.dealPageFilterFn(this.pageInfo, this.operationType, 'operationType', FilterEnum.EQUALS, FilterTypeEnum.AND);
    this.dealPageFilterFn(this.pageInfo, this.moduleName, 'moduleName', FilterEnum.CONTAINS, FilterTypeEnum.AND);
    this.dealPageFilterFn(this.pageInfo, result, 'gmtCreate', FilterEnum.BETWEEN, FilterTypeEnum.AND);
    this.getPage();
  }

  dealPageFilterFn(pageParam: PageEntity, currVal: any, strVal: string, filterMode: any, typeMode: any): void {
    currVal ? pageParam.filters[strVal.toString()] = { matchMode: filterMode, value: currVal, whereType: typeMode } :
      pageParam.filters[strVal.toString()] = undefined;
  }

  // 点击重置
  resetSearch() {
    this.operator = null;
    this.operationType = null;
    this.moduleName = null;
    this.gmtCreate = null;
    this.date = null;
    this.pageInfo = new PageEntity();
    this.pageInfo.first = 1;
    this.pageInfo.filters['operator'.toString()] = undefined;
    this.pageInfo.filters['operationType'.toString()] = undefined;
    this.pageInfo.filters['moduleName'.toString()] = undefined;
    this.pageInfo.filters['gmtCreate'.toString()] = undefined;
    this.getPage();
  }

  // 分页查询
  getPage() {
    const data = this.gmtCreate ? this.gmtCreate : new Date();
    const temp = this.datePipe.transform(data, 'yyyy-MM-dd');
    const result = temp + ' 00:00:00,' + temp + ' 23:59:59';
    this.dealPageFilterFn(this.pageInfo, result, 'gmtCreate', FilterEnum.BETWEEN, FilterTypeEnum.AND);
    this.service.page(this.pageInfo).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.allList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
      }
    });
    console.log(this.gmtCreate)
  }

  // 第几页
  searchPageIndex(event) {
    this.pageInfo.first = event;
    this.getPage();
  }

  // 每页几条
  searchPageSize(event) {
    this.pageInfo.rows = event;
    this.pageInfo.first = 1;
    this.getPage();
  }

  currentPageDataChange($event): void {
    this.displayList = $event;
    this.refreshStatus();
  }

  refreshStatus(): void {
    this.isAllCheck = this.displayList.every(item => this.mapOfCheckedId[item.id]);
    this.isIndeterminate =
      this.displayList.some(item => this.mapOfCheckedId[item.id]) &&
      !this.isAllCheck;
    this.checkDelete();
  }

  checkAll(value: boolean): void {
    this.displayList.forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }


  /**
   * 导出Excel文件
   */
  downloadTable() {
    this.postDown.postDownload(`${SERVERS_NAME.SECURITY_SERVER}sys/log/download`, this.pageInfo);
  }
}

